package com.itheima.task;

import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MyTask {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AliOssUtil aliOssUtil;

    @Scheduled(cron = "59 59 23 * * ? ")
    public void print() {

        log.info("定时任务执行");
        //获取两个集合的差集//调用阿里云oss工具类删除
       if(redisTemplate.getKeySerializer()!=null){
           redisTemplate.opsForSet().difference("imgUrl", "imgUrlend").forEach(x -> {
               int index = x.toString().lastIndexOf("/");

               aliOssUtil.delete(x.toString().substring(index + 1));

               redisTemplate.opsForSet().remove("imgUrl", x);
               log.info("删除成功{}", x.toString().substring(index + 1));
           });
       }


    }
}
